using System;
using System.Web;
using System.Web.Security;

namespace System.Web.Core
{
    public class CookieManager
    {
        private CookieManager()
        {
        }

        public static string GetCookieValue(string cookieName, string defaultValue)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];

            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                return defaultValue;
            }

            return cookie.Value;
        }
        public static void SaveCookie(string cookieName, string cookieValue, int expireDays)
        {
            HttpCookie cookie = HttpContext.Current.Response.Cookies[cookieName];

            if (cookie == null || string.IsNullOrEmpty(cookie.Value))
            {
                cookie = new HttpCookie(cookieName);
            }

            cookie.Value = cookieValue == null ? "" : cookieValue.ToString();
            SaveCookie(cookie, expireDays);
        }
        public static void SaveCookie(HttpCookie cookie, int expireDays)
        {
            cookie.Domain = FormsAuthentication.CookieDomain;
            cookie.Expires = DateTime.Now.AddDays(expireDays);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        public static void SaveCookie(HttpCookie cookie)
        {
            cookie.Domain = FormsAuthentication.CookieDomain;
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        public static void DeleteCookie(HttpCookie cookie)
        {
            if (cookie != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
                cookie.Expires = new DateTime(1999, 10, 12);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
    }
}